package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import websocket.ChatConnectionPool;

import db.HibernateUtil;
import db.HooldusteenuseTellimus;

/**
 * Servlet implementation class OutsideServiceController
 */
@WebServlet("/muu")
public class OtherServiceController extends BaseController implements Servlet {
	private static final long serialVersionUID = 1L;
       
    @Override
    protected void get(HttpServletRequest request, HttpServletResponse response, String username) throws ServletException, IOException {
	RequestDispatcher view = request.getRequestDispatcher("/other.jsp");
	request.setAttribute("kinnistud", HibernateUtil.getKinnistud(username));
	request.setAttribute("teenused", HibernateUtil.getHooldusteenused("muu"));
	view.forward(request, response);	
    }

    @Override
    protected void post(HttpServletRequest request, HttpServletResponse response, String username) throws ServletException, IOException {
	HooldusteenuseTellimus t = FormUtil.getTellimusFromRequest(request, HibernateUtil.getSessionFactory().openSession());
	HibernateUtil.insertHooldusteenuseTellimus(t);
	ChatConnectionPool.sendNotification(t);
	response.sendRedirect("/websocket/pealeht");	
    }

}
